# -*- coding: utf-8 -*-
import urllib2, urllib, re, string, sys, os, gzip, StringIO


# Plugin constants
UserAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3'


def GetHttpData(url):
    print "getHttpData: " + url
    req = urllib2.Request(url)
    req.add_header('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)')
    try:
        response = urllib2.urlopen(req)
        httpdata = response.read()
        if response.headers.get('content-encoding', None) == 'gzip':
            httpdata = gzip.GzipFile(fileobj=StringIO.StringIO(httpdata)).read()
        charset = response.headers.getparam('charset')
        response.close()
    except:
        print 'GetHttpData Error: %s' % url
        return ''
    match = re.compile('<meta http-equiv=["]?[Cc]ontent-[Tt]ype["]? content="text/html;[\s]?charset=(.+?)"').findall(httpdata)
    if len(match)>0:
        charset = match[0]
    if charset:
        charset = charset.lower()
        if (charset != 'utf-8') and (charset != 'utf8'):
            httpdata = httpdata.decode(charset, 'ignore').encode('utf8', 'ignore')
    return httpdata

url = "http://www.moko.cc/moko/post/1.html"
link = GetHttpData(url)
match0 = re.compile('<div class="w970">(.+?)<!-- 展示模块 end -->', re.DOTALL).search(link)
match_url = re.compile('<a href="(/post/\d*\.html)"').findall(match0.group(1))
match_name = re.compile('class="nickname" >(.+?)</a>').findall(match0.group(1))
totalItems = len(match_url)


# print link
# print "ash:" + match0.group(0)
# print match0.group(1)

# print totalItems
# print match_url
# print match_name

url = 'http://www.moko.cc/post/778083.html'
link = GetHttpData(url)

match00 = re.compile('<div class="article" id="postContentDiv">(.+?)<div class="dBd">', re.DOTALL).search(link)
match = re.compile('<img src2="(http://.*\.jpg)"').findall(match00.group(1))

for i in match :
	print i